/**
 *  Copyright (C) 2010  Thomas Volk
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.bookmarks.core.service;

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.bookmarks.core.beans.ContentType;
import com.googlecode.bookmarks.core.beans.MimeType;
import com.googlecode.bookmarks.core.exceptions.EntityNotFoundException;

@Transactional
public class DatabaseInitialisationServiceImpl extends
		AbstractPersistenceService implements DatabaseInitialisationService {
	@Autowired
	private ContentTypeService contentTypeService;
    @Autowired
    private AutentificationService autentificationService;
	private Map<String, String> defaultContentTypes;

	public Map<String, String> getDefaultContentTypes() {
		return defaultContentTypes;
	}

	public void setDefaultContentTypes(Map<String, String> defaultContentTypes) {
		this.defaultContentTypes = defaultContentTypes;
	}

	public ContentTypeService getContentTypeService() {
		return contentTypeService;
	}

    public AutentificationService getAutentificationService() {
        return autentificationService;
    }

    public void setAutentificationService(AutentificationService autentificationService) {
        this.autentificationService = autentificationService;
    }

    public void initialize() {
		for (Entry<String, String> item : getDefaultContentTypes().entrySet()) {
			String name = item.getKey();
			String mimeTypes = item.getValue();
			updateContentType(name, mimeTypes);
		}
        try {
            getAutentificationService().getUserByLogin("guest");
        } catch (EntityNotFoundException e) {
            getAutentificationService().createUser("guest", "guest");
        }
    }


    private void updateContentType(String name, String mimeTypes) {
		ContentType contentType;
		try {
			contentType = getContentTypeService().findContentTypeByName(name);
		} catch (EntityNotFoundException e) {
			MimeType mimeType;
			try {
				mimeType = getContentTypeService().findMimeTypeTypeByTypes(
						mimeTypes);
			} catch (EntityNotFoundException e1) {
				mimeType = new MimeType(mimeTypes);
				getEntityManager().persist(mimeType);
			}
			contentType = new ContentType(name, mimeType);
			getEntityManager().persist(contentType);
		}
	}

}
